﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace NMF.Expressions
{
    internal static class ReflectionExtension
    {
        public static MethodInfo GetStaticMethod(this TypeInfo type, string name, params Type[] parameterTypes)
        {
            return type.GetDeclaredMethods(name).FirstOrDefault(m =>
                (m.Attributes & (MethodAttributes.Public | MethodAttributes.Static)) == (MethodAttributes.Public | MethodAttributes.Static)
                && FitsParameters(m, parameterTypes));
        }

        public static bool FitsParameters(MethodInfo method, params Type[] parameterTypes)
        {
            if (method == null) throw new ArgumentNullException("method");

            var pars = method.GetParameters();
            if (pars.Length != parameterTypes.Length) return false;

            for (int i = 0; i < pars.Length; i++)
            {
                if (pars[i].ParameterType.GetTypeInfo().IsAssignableFrom(parameterTypes[i].GetTypeInfo())) return false;
            }
            return true;
        }
    }
}
